<template>
  <div>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="请选择姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-col :span="11">
          <el-date-picker v-model="form.timeOfEntry" format="yyyy年MM月dd日" value-format="yyyy-MM-dd" type="date" placeholder="请选择入职时间" style="width: 100%;" />
        </el-col>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option v-for="item in ConstantEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="form.departmentName" placeholder="请输入部门" readonly @focus="isTree=true" />
        <el-tree v-if="isTree" :data="data" :props="defaultProps" @node-click="handleNodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-col :span="11">
          <el-date-picker v-model="form.correctionTime" format="yyyy年MM月dd日" value-format="yyyy-MM-dd" type="date" placeholder="请选择转正时间" style="width: 100%;" />
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import ConstantEnum from '@/constant/employees'
import { valideMobile } from '@/utils/validate'
import { getDpmRequest } from '@/api/department'
import { transformListToTree } from '@/utils'
import { addUserRequest } from '@/api/employees'
export default {
  name: '',
  data() {
    // 封装校验函数
    var validatePass = (rule, value, callback) => {
      // 调用封装在utils/validate中的valideMobile方法完成对输入数据的校验
      if (!valideMobile(value)) {
        // 不符合校验时提示用户
        callback(new Error('请输入正确手机号格式!'))
        // Message.error('格式错误!请仔细检查格式.')
      } else {
        callback()
      }
    }
    return {
      ConstantEnum: ConstantEnum,
      form: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '请输入正确姓名', trigger: 'blur' },
          { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'change' }
        ],
        mobile: [
          { required: true, message: '请选择活动区域', trigger: 'blue' },
          { validator: validatePass, trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职日期', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正日期', trigger: 'change' }
        ],
        formOfEmployments: [
          { type: 'array', required: true, message: '请至少选择一个部门', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '请填写正确工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请填写所在部门', trigger: 'blur' }
        ]
      },
      // el-tree 需要的变量
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      isTree: false

    }
  },
  created() {
    this.getDepartmentInfo()
  },
  methods: {
    // 提交按钮功能
    onSubmit() {
    //   console.log(this.form)
      //   this.addUser()
      this.$refs.formRef.validate(val => {
        // console.log(val)
        if (val) {
          this.addUser()
        }
      })
    },
    // 获取部门结构信息功能
    async getDepartmentInfo() {
      try {
        const res = await getDpmRequest()
        res.data.depts.shift()
        this.data = transformListToTree(res.data.depts, '')
        console.log('getDpmRequest: ', res)
      } catch (err) {
        console.log('getDpmRequest: ', err)
      }
    },
    // 添加员工信息功能
    async addUser() {
      try {
        const res = await addUserRequest(this.form)
        if (res.success) {
          this.$emit('success')
          this.reset()
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('addUserRequest: ', err)
      }
    },
    // 获取部门节点信息功能
    handleNodeClick(val) {
      // console.log(val)
      if (val.children?.length > 0) {
        return
      }
      this.form.departmentName = val.name
      this.isTree = false
    },
    // 取消按钮功能
    cancel() {
      this.$emit('close')
    },
    // 表单清空功能
    reset() {
      this.$refs.formRef.resetFields()
      this.form = this.$options.data().form
    }
  }
}
</script>
